<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : sendsitemail.class.php   FILE_PATH : \classes\libraries\sendsitemail.class.php
 * ....根据后台的设置，发送邮件类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Fri Nov 09 17:26:39 CST 2007
 */

require_once("mailcontent.class.php");

class SendSiteMail extends BaseInitialize {
	/**
	 * 邮件内容类
	 *
	 * @var object
	 */
	var $objcontent;
	var $smtpconfig;
	
	/**
	 * 初始化网站发送邮件类
	 *
	 * @return SendSiteMail
	 */
	function SendSiteMail(){
		/**
		 * 创建网站邮件发送内容信息对象
		 */
		if (!is_object($this->objcontent)){
			require_once("mailcontent.class.php");
			$this->objcontent = new MailContentClass();
		}
	}
	
	/**
	 * 发送邮件
	 * @author workflow Develop Team
	 * @param  $return true 为由返回，默认为发送邮件
	 * @return int/bool/object/array
	 */
	function SendMail($module_name,$value_array,$send_email,$return = ""){
		//判断邮件帐号是否为空
		if ($this->smtpconfig['websit']['smtpuser'] == '' || $this->smtpconfig['websit']['smtppass'] == ''){
			return false;
		}
		/**
		 * 获取功能模块发送信笺的设置信息
		 */
		$content = $this->objcontent->getMailContent($module_name);
		if ($return == true){
			return $content;
		}else {
			/**
				 * 需要转换的变量及值，表现形式为一维数组，键为要替换的变量，值为替换的值
				 */
			$default_array = array(
			'website'=>$this->smtpconfig['websit']['site_title'],
			'site_url'=>$this->smtpconfig['websit']['site_url'],
			'sitename'=>$this->smtpconfig['websit']['site_name'],
			);
			$param = array_merge($default_array,$value_array);

			//对邮件内容及标题进行替换
			$content['content'] = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><meta http-equiv="Content-Type" content="text/html; charset='.$this->smtpconfig['websit']['ncharset'].'" /><HEAD></HEAD><BODY>'.Common::replaceMailContent($param,$content['content']).'</BODY></HTML>';   //将发信内容的变量换为值
			$content['title'] = Common::replaceMailContent($param,$content['title']);
			$content['title'] = "=?".$this->smtpconfig['websit']['ncharset']."?B?".base64_encode($content['title'])."?=";

			//模块开启了发邮件功能
			if ($content['ifopen'] == '1'){
				//取邮件类别设置
				require_once('settings.class.php');
				$obj_settings = new SettingsClass();
				$email_send_type = $obj_settings->getSettings('email_send_type');
				unset($obj_settings);
				//判断
				if($email_send_type == '0'){
					//直接用mail
					$maildelimiter = "\r\n";
					$charset = $this->smtpconfig['websit']['ncharset'];
					$email_from = $this->smtpconfig['websit']['smtpemail'];
					$headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: ShopNc {$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/html;charset=$charset{$maildelimiter}";
					//charset=$charset{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}
					@mail($send_email, $content['title'], $content['content'], $headers);
				}else if($email_send_type == '1'){
					/**
						 * 创建发信对象
					     */
					if (!is_object($this->objmail)){
						include_once("sendmailer.class.php");
						$this->objmail = new SendMailer($this->smtpconfig['websit']['site_title'], $this->smtpconfig['websit']['smtpemail'], 1, $this->smtpconfig['websit']['smtpserver'], '25', $this->smtpconfig['websit']['smtpuser'], $this->smtpconfig['websit']['smtppass']);
					}
					//"发送信件到指定邮箱中";
					$this->objmail->send($send_email,$content['title'],$content['content'], $this->smtpconfig['websit']['ncharset'], 1);
				}
				return true;
			}else {
				return false;
			}
		}
	}
}
?>